package com.eshore.thrpc.server.thrift;

import org.apache.thrift.TProcessor;

import com.eshore.thrpc.RpcException;
import com.eshore.thrpc.common.ServerNode;
import com.eshore.thrpc.server.IServer;

/**
 * 
 * 创建服务工厂类
 * 
 *   
 * @author zhouzhixiang  
 * @date 2016年12月19日 下午3:42:58 
 * @since 1.0
 */
public class ThriftServerFactory {
    
    /**
     * 创建阻塞服务
     * 
     * @param processor {@link TProcessor}
     * @param serverNode {@link ServerNode}
     * @param maxWorkerThreads 最大工作线程数
     * @param minWorkerThreads 最小工作线程数
     * @throws RpcException
     */
    public static IServer createBlockingServer(TProcessor processor, ServerNode serverNode,
            int maxWorkerThreads, int minWorkerThreads) throws RpcException {
        ThriftServer server = new ThriftServer();
        TServerThread serverThread = new TServerThread();
        server.setServerThread(serverThread);
        serverThread.createBlockServer(processor, serverNode, maxWorkerThreads, minWorkerThreads);
        return server;
    }

    /**
     * 创建非阻塞服务
     * 
     * 
     * @author zhouzhixiang
     * @param processor
     * @param serverNode
     * @param selectorThreads
     * @param workerThreads
     * @param acceptQueueSizePerThread
     * @return
     * @throws RpcException
     */
    public static IServer createNonBlockingServer(TProcessor processor, ServerNode serverNode,
            int selectorThreads, int workerThreads, int acceptQueueSizePerThread)
            throws RpcException {
        ThriftServer server = new ThriftServer();
        TServerThread serverThread = new TServerThread();
        server.setServerThread(serverThread);
        serverThread.createNonBlockThread(processor, serverNode, selectorThreads, workerThreads,
                acceptQueueSizePerThread);
        return server;
    }
}
